
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:video_chewie_mutant/video_chewie_mutant.dart';
import 'package:video_player/video_player.dart';

void main() {
  runApp(
    const ChewieDemo(),
  );
}
String url30fps = 'https://ali-video-test.dabanjia.com/video/20210412/10/1618195519786_1893%24%E5%BC%82%E5%B8%B81.mp4';
String landUrl60fps = 'https://ali-video-test.dabanjia.com//video/20210311/1615414994676_5321%24%E5%88%86%E6%AE%B5%E5%BE%AE%E8%A3%85%E9%A4%90%E5%8E%A8%E4%B8%80%E4%BD%93%E7%94%B5%E5%95%86%E7%89%88_dDmV0mt7BE%E3%80%90%E8%80%81%E7%89%88%E6%9C%AC%E3%80%91_%E6%9D%8E%E7%85%9C_20210310.mp4';


class ChewieDemo extends StatefulWidget {
  // ignore: use_key_in_widget_constructors
  const ChewieDemo({this.title = 'Chewie Demo'});

  final String title;

  @override
  State<StatefulWidget> createState() {
    return _ChewieDemoState();
  }
}

class _ChewieDemoState extends State<ChewieDemo> {
  TargetPlatform _platform;
  VideoPlayerController _videoPlayerController1;
  //VideoPlayerController _videoPlayerController2;
  ChewieController _chewieController;

  @override
  void initState() {
    super.initState();
    initializePlayer();
  }

  @override
  void dispose() {
    _videoPlayerController1.dispose();
    //_videoPlayerController2.dispose();
    _chewieController.dispose();
    super.dispose();
  }

  Future<void> initializePlayer() async {
    _videoPlayerController1 = VideoPlayerController.network(
         url30fps);
    await _videoPlayerController1.initialize();
    // _videoPlayerController2 = VideoPlayerController.network(
    //     url30fps);
    // await _videoPlayerController2.initialize();
    _chewieController = ChewieController(
      changeTargetRatio:  0.4,
      overlay: const Positioned(left: 20,top: 60,child: Icon(Icons.arrow_back_ios_outlined,color: Colors.red,),),
      rotatedOverlay: const Positioned(left: 60,top: 20,child: Icon(Icons.arrow_back_ios_outlined,color: Colors.red,),),
      videoPlayerController: _videoPlayerController1,
      autoPlay: true,
      looping: true,
      // Try playing around with some of these other options:

      // showControls: false,
      // materialProgressColors: ChewieProgressColors(
      //   playedColor: Colors.red,
      //   handleColor: Colors.blue,
      //   backgroundColor: Colors.grey,
      //   bufferedColor: Colors.lightGreen,
      // ),
      // placeholder: Container(
      //   color: Colors.grey,
      // ),
      // autoInitialize: true,
    );
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: widget.title,
      theme: ThemeData.light().copyWith(
        platform: _platform ?? Theme.of(context).platform,
      ),
      home: WillPopScope(
        onWillPop: () {
          final size = _chewieController.videoPlayerController.value.size;
          if(_chewieController.isFullScreen ) {
            _chewieController.exitFullScreen();
            return Future.value(false);
          } else {
            return Future.value(true);
          }
        },
        child: Scaffold(
          // appBar: AppBar(
          //   title: Text(widget.title),
          // ),
          body: Column(
            children: <Widget>[
              Expanded(
                child: Center(
                  child: _chewieController != null &&
                      _chewieController
                          .videoPlayerController.value.initialized
                      ? Chewie(
                    controller: _chewieController,
                  )
                      : Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: const [
                      CircularProgressIndicator(),
                      SizedBox(height: 20),
                      Text('Loading'),
                    ],
                  ),
                ),
              ),
              // FlatButton(
              //   onPressed: () {
              //     _chewieController.enterFullScreen();
              //   },
              //   child: const Text('Fullscreen'),
              // ),
              // Row(
              //   children: <Widget>[
              //     Expanded(
              //       child: FlatButton(
              //         onPressed: () {
              //           setState(() {
              //             _chewieController.dispose();
              //             _videoPlayerController1.pause();
              //             _videoPlayerController1.seekTo(const Duration());
              //             _chewieController = ChewieController(
              //               videoPlayerController: _videoPlayerController1,
              //               autoPlay: true,
              //               looping: true,
              //             );
              //           });
              //         },
              //         child: const Padding(
              //           padding: EdgeInsets.symmetric(vertical: 16.0),
              //           child: Text("Landscape Video"),
              //         ),
              //       ),
              //     ),
              //     Expanded(
              //       child: FlatButton(
              //         onPressed: () {
              //           setState(() {
              //             _chewieController.dispose();
              //             _videoPlayerController2.pause();
              //             _videoPlayerController2.seekTo(const Duration());
              //             _chewieController = ChewieController(
              //               videoPlayerController: _videoPlayerController2,
              //               autoPlay: true,
              //               looping: true,
              //             );
              //           });
              //         },
              //         child: const Padding(
              //           padding: EdgeInsets.symmetric(vertical: 16.0),
              //           child: Text("Portrait Video"),
              //         ),
              //       ),
              //     )
              //   ],
              // ),
              // Row(
              //   children: <Widget>[
              //     Expanded(
              //       child: FlatButton(
              //         onPressed: () {
              //           setState(() {
              //             _platform = TargetPlatform.android;
              //           });
              //         },
              //         child: const Padding(
              //           padding: EdgeInsets.symmetric(vertical: 16.0),
              //           child: Text("Android controls"),
              //         ),
              //       ),
              //     ),
              //     Expanded(
              //       child: FlatButton(
              //         onPressed: () {
              //           setState(() {
              //             _platform = TargetPlatform.iOS;
              //           });
              //         },
              //         child: const Padding(
              //           padding: EdgeInsets.symmetric(vertical: 16.0),
              //           child: Text("iOS controls"),
              //         ),
              //       ),
              //     )
              //   ],
              // )
            ],
          ),
        ),
      ),
    );
  }
}
